package com.woniuxy.controller;

import com.woniuxy.dao.DrvingRecordMapper;
import com.woniuxy.pojo.*;
import com.woniuxy.service.DrvingRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.DrvingRecordVo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DrvingRecordController extends BaseController {
    @Autowired
    private DrvingRecordService drvingRecordService;
    @Autowired
    private DrvingRecordMapper drvingRecordMapper;

    /**
     * 查询所有及模糊查询
     *
     * @param drvingRecordVo
     * @return
     */
    @GetMapping(value = "/drvingRecords")
    public DataGridView selectDrvingRecordsByPage(DrvingRecordVo drvingRecordVo) {
        return drvingRecordService.selectDrvingRecordsByProperty(drvingRecordVo);
    }

    /**
     * 根据id删除出行记录
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/drvingRecords/{id}")
    public ResultDto deleteDrvingRecords(@PathVariable("id") int id) {
        try {
            this.drvingRecordService.deleteDrvingRecordById(id);
            return ResultDto.DELETE_DR_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_DR_FAILURE;
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/drvingRecords/deleteBathDrvingRecord")
    public ResultDto removeBatchDrvingRecord(@RequestParam("ids[]") Integer[] ids) {
        try {
            this.drvingRecordService.removeBatchDrvingRecord(ids);
            return ResultDto.DELETE_DR_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_DR_SUCCESS;
        }
    }

    @GetMapping("/drvingRecords/cars.do")
    public List<Car> cars() {
        return drvingRecordMapper.selectAllCars();
    }

    @GetMapping("/drvingRecords/drivers.do")
    public List<Driver> drivers() {
        return drvingRecordMapper.selectAllDrivers();
    }

    @GetMapping("/drvingRecords/depts.do")
    public List<Dictionary> depts() {
        return drvingRecordMapper.selectAllDepts();
    }

    @GetMapping("/drvingRecords/users.do")
    public List<User> users() {
        return drvingRecordMapper.selectAllUsers();
    }

    @PostMapping(value = "/drvingRecords.do")
    public ResultDto addDrvingRecord(DrvingRecord drvingRecord) {
//        DrvingRecord dr = drvingRecordMapper.findDrvingRecordByCarId(drvingRecord.getCar().getId());
//        if (dr!=null){
//            return ResultDto.DRIVER_EXITS;
//        }else{
        boolean flag = this.drvingRecordService.insertDrvingRecord(drvingRecord);
        if (flag) {
            return ResultDto.DRVINGRECORD_ADD_SUCCESS;
        } else {
            return ResultDto.DRVINGRECORD_ADD_FAIL;
        }
    }
    @PostMapping(value = "/drvingRecords/update.do")
    public ResultDto updateDrvingRecord(DrvingRecord drvingRecord){
        boolean flag = this.drvingRecordService.updataDrvingRecord(drvingRecord);
        if (flag){
            return ResultDto.DRVINGRECORD_UPDATE_SUCCESS;
        }else {
            return ResultDto.DRVINGRECORD_UPDATE_FAIL;
        }
    }
}
